//
//  Change_OrderStatus.swift
//  Pods
//
//  Created by eme on 2017/4/1.
//
//

import UIKit
import RxSwift
import ModelProtocol

class Change_OrderStatus{
    
    let changeOrderStatus_Cancel = PublishSubject<String>()
    
    let changeOrderStatus_Confirm = PublishSubject<String>()
    
    let backData = PublishSubject<OrderDetailsModel>()
    
    let disposeBag = DisposeBag()
    
    let error = PublishSubject<MikerError>()
    
    let refreshing = Variable(false)
    
    var iscanevaluate = ""
    
    var payid = ""
    
    init (){
        
        let request_Cancel = changeOrderStatus_Cancel.map{ orderid in
            
            return MineApi.changeOrderStatus_Cancel(orderid: orderid, iscanevaluate: self.iscanevaluate,payid:self.payid)
            
            }.shareReplay(1)
        
        let response_Cancel = request_Cancel.emeRequestApiForObj(OrderDetailsModel.self).shareReplay(1)
        
        response_Cancel.subscribe(onNext:{[unowned self] result in
            
            switch result{
                
            case .success(let data):
                
                self.backData.onNext(data)
                
            case.failure(let error):
                
                self.error.onNext(error)
                
            }
            
        }).addDisposableTo(disposeBag)
        
        let request_Confirm = changeOrderStatus_Confirm.map{ orderid in
            
            return MineApi.changeOrderStatus_Confirm(orderid:orderid,iscanevaluate: self.iscanevaluate)
            
            }.shareReplay(1)
        
        let response_Confirm = request_Confirm.emeRequestApiForObj(OrderDetailsModel.self).shareReplay(1)
        
        response_Confirm.subscribe(onNext:{[unowned self] result in
            
            switch result{
                
            case .success(let data):
                
                self.backData.onNext(data)
                
            case.failure(let error):
                
                self.error.onNext(error)
                
            }
            
        }).addDisposableTo(disposeBag)
        
        //
        
        //        Observable
        //            .of(
        //                request.map { _ in true },
        //                response.map { _ in false },
        //                error.map { _ in false }
        //            )
        //            .merge()
        //            .bindTo(refreshing)
        //            .addDisposableTo(disposeBag)
    }
    
}
